package com.persicadev.khayyam;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Vector;

import com.persicadev.khayyam.R;
import com.persicadev.khayyam.IPoemDb.Poem;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.DataSetObserver;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class SearchActivity extends Activity implements OnEditorActionListener, OnItemClickListener, ListAdapter {

	public static final String KEY_BOOKMARKS = "bookmarks";
	
	private boolean isBookmarks;
	
	private SharedPreferences sp;
	private Vector<DataSetObserver> observers = new Vector<DataSetObserver>(2);
	private List<Poem> results = null;
	
	private Typeface typeFace;
	
	public static List<Poem> loadBookmarks(SharedPreferences sp) {
		Map<String, ?> ids = sp.getAll();
		List<Poem> results = new Vector<IPoemDb.Poem>(20);
		for (Entry<String, ?> entry : ids.entrySet()) {
			results.add(KhayyamActivity.poemDb.getPoem(Integer.valueOf(entry.getKey())));
		}
		return results;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.searchpoems);
		isBookmarks = getIntent().getBooleanExtra(KEY_BOOKMARKS, false);
		sp = getSharedPreferences(KhayyamActivity.PREFERENCES, MODE_PRIVATE);
		typeFace = Typeface.createFromAsset(getAssets(), "fonts/BZar.ttf");
		EditText edit = (EditText) findViewById(R.id.search_text);
		if (isBookmarks) {
			edit.setVisibility(View.GONE);
		}
		else {
			edit.setTypeface(typeFace);
			edit.setOnEditorActionListener(this);
		}
		ListView list = (ListView) findViewById(R.id.search_result);
		list.setAdapter(this);
		list.setOnItemClickListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (isBookmarks) results = loadBookmarks(sp);
		for (DataSetObserver observer : observers) observer.onChanged();
	}

	public void onItemClick(AdapterView<?> parent, View child, int position, long id) {
		startActivity(new Intent(this, RandomActivity.class).putExtra(RandomActivity.KEY_ID, results.get(position).getId()));
		overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
	}

	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		String text = v.getText().toString().trim();
		if (text.length()>0) {
			results = KhayyamActivity.poemDb.search(text);
			for (DataSetObserver observer : observers) observer.onChanged();
		}
		return true;
	}

	public int getCount() {
		if (results!=null) return results.size();
		return 0;
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public int getItemViewType(int position) {
		return IGNORE_ITEM_VIEW_TYPE;
	}

	private void setup(TextView view, String string) {
		view.setTypeface(typeFace);
		view.setText(PersianReshape.reshape(string));
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		Poem poem = results.get(position);
		RelativeLayout child = (RelativeLayout) View.inflate(this, R.layout.listpoemitem, null);
		setup((TextView) child.findViewById(R.id.item_verse1), poem.getVerse(0).getPart1());
		setup((TextView) child.findViewById(R.id.item_verse2), poem.getVerse(0).getPart2());
		setup((TextView) child.findViewById(R.id.item_verse3), poem.getVerse(1).getPart1());
		setup((TextView) child.findViewById(R.id.item_verse4), poem.getVerse(1).getPart2());
		if (!isBookmarks && sp.contains(String.valueOf(position).trim())) child.setBackgroundColor(0x4400ff00);
		return child;
	}

	public int getViewTypeCount() {
		return 1;
	}

	public boolean hasStableIds() {
		return false;
	}

	public boolean isEmpty() {
		if (results!=null) return results.size()==0;
		return true;
	}

	public void registerDataSetObserver(DataSetObserver observer) {
		observers.add(observer);
	}

	public void unregisterDataSetObserver(DataSetObserver observer) {
		observers.remove(observer);
	}

	public boolean areAllItemsEnabled() {
		return true;
	}

	public boolean isEnabled(int position) {
		return true;
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
	}

}
